home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c3 / pro24 / phase2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-07  |  10.1 KB  |  342 lines

  1. /* phase2.c -- this module plays notes compiled and sorted in phase1 */
  2.  
  3. /*****************************************************************************
  4. *        Change Log
  5. *  Date        | Change
  6. *-----------+-----------------------------------------------------------------
  7. * 31-Dec-85 | Created changelog
  8. * 31-Dec-85 | Add c:\ to include directives
  9. * 31-Dec-85 | Changed to use new CBREAK as well as kbhit
  10. *  1-Jan-86 | Added stop_reason
  11. *  1-Jan-86 | <rgd/jmn> Require musicfns.h before adagio.h
  12. *        | stop_time is now long
  13. *        | Turn off all notes in moreclean
  14. *        | changed control logic in play
  15. *  1-Jan-86 | Added miditrace
  16. *  1-Jan-86 | Added command scanner at play request level
  17. * 18-Jan-86 | Gave priority to note offs.
  18. *        | Created stop_check routine.
  19. * 21-Jan-86 | Added mpu_error_check to main loop
  20. *  3-Feb-86 | Changed help message slightly
  21. * 24-Mar-86 | Changed representation from note_struct to event_struct
  22. * 16-Jul-86 | Only pass upper case letters to tolower()
  23. *  7-Aug-86 | Initialize bend, touch, porta, mod, and foot controls
  24. *****************************************************************************/
  25.  
  26. #include "cext.h"
  27. #include "stdio.h"
  28. #include "adagio.h"
  29. #include "mpu.h"
  30. #include "userio.h"
  31. #include "phase2.h"
  32.  
  33. #define stop_code ' '
  34. char stop_explanation[] = "Space bar";
  35.  
  36. extern int musictrace;
  37. extern int miditrace;
  38. extern int CBREAK;
  39.  
  40. #define STOP_CC 1
  41. #define STOP_CBRK 2
  42. #define STOP_KEY 3
  43.  
  44. private long offset = 100;    /* time offset from clock to performance */
  45.  
  46. private int program[num_voices];    /* current program */
  47.  
  48. /****************************************************************************
  49. * Routines local to this module
  50. ****************************************************************************/
  51. private    void    init_stuff();
  52. private    void    moreclean();
  53. private    void    play();
  54. private    void    play_help();
  55. private    void    print_status();
  56. private boolean stop_check();
  57.  
  58. /****************************************************************************
  59. *                  init_stuff
  60. * Effect: initializes this module (called by phase2()).
  61. ****************************************************************************/
  62.  
  63. private void init_stuff()
  64. {
  65.     off_init();
  66. }
  67.  
  68. /****************************************************************************
  69. *                moreclean
  70. * Inputs:
  71. *    event_type root: the score that was in progress
  72. *    int stop_reason: the reason the score was stopped
  73. * Effect: 
  74. *    prints a message to announce stopped state
  75. *    tells what was playing at the stop time
  76. * Implementation:
  77. *    What was playing is determined by reading the time and searching
  78. *    for notes that are on at that time.  If nothing is found on a
  79. *    given channel, it would be nice to print the last note played
  80. *    on the channel provided it was played within say 1 sec of the
  81. *    stop time.  This is unimplemented, but would help locate short
  82. *    notes.
  83. ****************************************************************************/
  84.  
  85. private void moreclean(score, stop_reason)
  86.     event_type score;
  87.     int stop_reason;
  88. {
  89.     long stop_time;    /* time at which moreclean is called */
  90.     event_type n;    /* used to search the score */
  91.  
  92.     stop_time = gettime() - offset;    /* get current time */
  93.  
  94.     while (note_offs(0x7fffffff)) ;    /* turn off all notes */
  95.  
  96.     printf ("\n         * * STOPPED * *\n");
  97.  
  98.     switch(stop_reason)
  99.        { /* reason for stopping */
  100.     case STOP_CC:    printf("Control-C seen\n");
  101.             break;
  102.     case STOP_CBRK: printf("Control-break seen\n");
  103.             break;
  104.     case STOP_KEY:    printf("%s hit\n",stop_explanation);
  105.             break;
  106.        } /* reason for stopping */
  107.  
  108.     if (stop_time > 0)
  109.     printf ("Stopped at time = %ld (hundreths of a second). \n\n",
  110.         stop_time);
  111.  
  112.     for (n = score; n != NULL; n = n->next) {
  113.     if (is_note(n) &&
  114.         (n->ntime <= stop_time) &&
  115.         (n->ntime + n->u.note.ndur) >= stop_time) {
  116.         printf("Voice: %d was playing line: %d\n",
  117.         n->nvoice + 1, n->nline);
  118.     }
  119.     }
  120. }
  121.  
  122. /****************************************************************************
  123. *                    phase2
  124. * Inputs:
  125. *    event_type root: Root of play list
  126. * Effect: 
  127. *    Plays the music
  128. ****************************************************************************/
  129.  
  130. void phase2(root)
  131.     event_type root;
  132. {
  133.     char resp;
  134.     short done = false;
  135.     int truth;
  136.  
  137.     init_stuff();
  138.     while (!done) { /* ask user */
  139.     truth = true;
  140.     printf ("\nType <RETURN> to play notes, q<RETURN> to quit, ?<RETURN> for help:");
  141.     while (true) { /* read input */
  142.         resp = getchar();
  143.         if (isupper(resp)) resp = tolower(resp);
  144.         switch (resp) { /* decode */
  145.         case ' ':
  146.             continue;    /* ignore spaces */
  147.         case '?': 
  148.             play_help();
  149.             readln(stdin);
  150.             break;
  151.         case '-':
  152.             truth = !truth;
  153.             continue;    /* read next char */
  154.         case 't':
  155.             trace(truth);
  156.             readln(stdin);
  157.             break;
  158.         case 'm':
  159.             tracemidi(truth);
  160.             readln(stdin);
  161.             break;
  162.         case 'q':
  163.             done = true;
  164.             readln(stdin);
  165.             break;
  166.         case 'r':
  167.             readln(stdin);
  168.             read_tuning("");
  169.             break;
  170.         case 's':
  171.             print_status();
  172.             readln(stdin);
  173.             break;
  174.         case '\n':
  175.             CBREAK = 0;
  176.             play(root);
  177.             break;
  178.         } /* decode */
  179.         break;
  180.     } /* read input */
  181.     } /* ask user */
  182. }
  183.  
  184. /****************************************************************************
  185. *                     play
  186. * Inputs:
  187. *    event_type score: Score to play
  188. * Effect: 
  189. *    Plays the score
  190. ****************************************************************************/
  191.  
  192. private void play(score)
  193.     event_type score;
  194. {    
  195.     event_type event = score;    /* pointer to next note or control event */
  196.     short done = false;
  197.     int stop_reason;        /* ctrl-C, break, or space bar */
  198.     long time;            /* the current time */
  199.     int i;            /* index counter to initialize channels */
  200.  
  201.     printf("Type %s to stop.\n",stop_explanation);
  202.  
  203.     musicinit();  
  204.  
  205.     /* Initialize all midi channels with reasonable start values: */
  206.     for (i = 1; i <= num_voices; i++) {
  207.     midi_program(i, 1);
  208.     program[i - 1] = 1;
  209.     midi_bend(i, 1 << 13);
  210.     midi_touch(i, 0);
  211.     midi_ctrl(i, PORTARATE, 99);
  212.     midi_ctrl(i, PORTASWITCH, 0);
  213.     midi_ctrl(i, MODWHEEL, 0);
  214.     midi_ctrl(i, FOOT, 99);
  215.     }
  216.  
  217.     timereset();
  218.     l_restuntil(offset);
  219.  
  220.     while (!done) { /* play it, Sam */
  221.     time = gettime() - offset; /* delay everything by offset */
  222.     done = true;
  223.     if (note_offs(time)) done = false;    /* still more note offs */
  224.     if (event != NULL) {  /* something to play */
  225.         done = false;
  226.         if (time >= event->ntime) {
  227.         if (is_note(event)) { /* play a note */
  228.             /* check for correct program (preset) */
  229.             if (event->u.note.nprogram != program[event->nvoice]) {
  230.             midi_program(event->nvoice+1, event->u.note.nprogram);
  231.              program[event->nvoice] = event->u.note.nprogram;
  232.             }
  233.             /* if it is a note (not a rest) play it */
  234.             if (event->u.note.npitch != NO_PITCH) {
  235.             midi_note(event->nvoice+1, event->u.note.npitch,
  236.                   event->u.note.nloud);
  237.             off_schedule(event->ntime + event->u.note.ndur,
  238.                      event->nvoice, event->u.note.npitch);
  239.             }
  240.         } else {    /* send control info */
  241.             switch (vc_ctrl(event->nvoice)) {
  242.             case 1: midi_ctrl(vc_voice(event->nvoice) + 1,
  243.                          PORTARATE,
  244.                          event->u.ctrl.value);
  245.                 break;
  246.             case 2: midi_ctrl(vc_voice(event->nvoice) + 1,
  247.                          PORTASWITCH,
  248.                          event->u.ctrl.value);
  249.                 break;
  250.             case 3: midi_ctrl(vc_voice(event->nvoice) + 1,
  251.                          MODWHEEL,
  252.                          event->u.ctrl.value);
  253.                 break;
  254.             case 4: midi_touch(vc_voice(event->nvoice) + 1,
  255.                        event->u.ctrl.value);
  256.                 break;
  257.             case 5: midi_ctrl(vc_voice(event->nvoice) + 1,
  258.                          FOOT,
  259.                          event->u.ctrl.value);
  260.                 break;
  261.             case 6: midi_bend(vc_voice(event->nvoice) + 1,
  262.                       event->u.ctrl.value << 6);
  263.                 break;
  264.             default: break;
  265.             }
  266.         }
  267.         event = event->next;
  268.         } else if (CBREAK || kbhit()) {
  269.         done = stop_check(&stop_reason);
  270.         if (done) { /* clean up */
  271.             moreclean(score, stop_reason);
  272.         } /* clean up */
  273.         } else mpu_error_check();
  274.     } /* something to play */
  275.     } /* play it, Sam */
  276.  
  277.     musicterm();
  278. }
  279.  
  280. /****************************************************************************
  281. *                   play_help
  282. * Effect: 
  283. *    Lists help for play option
  284. ****************************************************************************/
  285.  
  286. private void play_help()
  287. {
  288.     fprintf(stderr," <return>       Play music\n");
  289.     fprintf(stderr," q<return>       Quit Adagio (Exit)\n\n");
  290.     fprintf(stderr," r<return>       Read tuning file\n");
  291.     fprintf(stderr," m<return>       Turn on MIDI byte trace\n");
  292.     fprintf(stderr,"-m<return>       Turn off MIDI byte trace\n");
  293.     fprintf(stderr," s<return>       Report state\n");
  294.     fprintf(stderr," t<return>       Turn on music operation trace\n");
  295.     fprintf(stderr,"-t<return>       Turn off music operation trace\n");
  296.     fprintf(stderr," ?<return>       This message\n");
  297. }
  298.  
  299. /****************************************************************************
  300. *                  print_status
  301. * Effect: 
  302. *    Informative output about state
  303. ****************************************************************************/
  304.  
  305. private void print_status()
  306. {
  307.     fprintf(stderr,"MIDI trace (m option) %s\n",(miditrace ? "on" : "off"));
  308.     fprintf(stderr,"Music trace (t option) %s\n",(musictrace ? "on" : "off"));
  309. }
  310.  
  311. /****************************************************************************
  312. *                     stop_check
  313. * Outputs:
  314. *    *reason is set to reason for stop
  315. *    true is returned iff play should stop
  316. * Effect: 
  317. *    Checks for break character or stop code from kbd
  318. ****************************************************************************/
  319.  
  320. private boolean stop_check(reason)
  321.     int *reason;
  322. {
  323.     boolean done = false;
  324.     switch (CBREAK) { /* stop reason */
  325.     case 0: /* no stop code, try keyboard */
  326.         done = (getch() == stop_code);
  327.         if (done) *reason = STOP_KEY;
  328.         break;
  329.     case 1: /* ctrl-break */
  330.         done = true;
  331.         if (kbhit()) getch();
  332.         *reason = STOP_CBRK;
  333.         break;
  334.     case 2: /* ctrl-C */
  335.         done = true;
  336.         *reason = STOP_CC;
  337.         if (kbhit()) getch();
  338.         break;
  339.     } /* stop reason */
  340.     return done;
  341. }
  342.